<template>
	<view class="detail page c00">
		<view class="bgf7 p12">
			<view class="bgff px14 pb10">
<!-- 
				<view class="flex flexC borb py12">
					<text class="flex1">价格</text>
					<input class="py3 aliL" v-model="price" />
					<text class="arrowR ml3"></text>
				</view> -->
				<view class="flex flexC borb py12" @click="chooseAddress">
					<text class="flex1">地址修改</text>
					<view class="w-10 flex flexC py3">
						<text class="flex1">{{address}}</text>
						<text class="arrowR ml3"></text>
					</view>
				</view>

				<view class="flex flexC borb py12">
					<text class="flex1">接单电话</text>
					<view class="w-10 flex flexC py3">
						<input class="py3 aliL" v-model="mobile" />
						<text class="arrowR ml3"></text>
					</view>
				</view>

				<view class="flex flexC borb py12">
					<text class="flex1">是否接单</text>
					<view class="w-10 flex flexC py3">
						<switch @change="takeSwitch" class="flex1 aliR" :checked="is_check" />
						<text class="arrowR ml3"></text>
					</view>
				</view>

				<view class="flex flexC borb py12" @click="clickWeek">
					<text class="flex1">周末时间</text>
					<view class="w-10 flex flexC py3">
						<text class="flex1">{{time}}</text>
						<text class="arrowR ml3"></text>
					</view>
				</view>


				<view class="flex flexC  py12" @click="clickDay">
					<text class="flex1">工作时间</text>
					<view class="w-10 flex flexC py3">
						<text class="flex1">{{time1}}</text>
						<text class="arrowR ml3"></text>
					</view>
				</view>


			</view>

			<view class="flex flexC flexJC py20">
				<view class="aliC py12 w-17 bg41 r40" @click="updateInfo">保存</view>
			</view>
		</view>


		<tpf-time-range ref="time" :startTime="weekTime1" :startDefaultTime="weekTime1" :endTime="weekTime2"
			:endDefaultTime="weekTime2" @timeRange="startRange">
		</tpf-time-range>

		<tpf-time-range ref="time2" :startTime="dayTime1" :startDefaultTime="dayTime1" :endTime="dayTime2"
			:endDefaultTime="dayTime2" @timeRange="startRange2">
		</tpf-time-range>
	</view>
</template>

<script>
	export default {
		components: {},

		data() {
			return {
				address: '',
				price: '',
				time: '',
				time1: '',
				mobile: '',
				is_take: 0,
				is_check: false,
				lat: '',
				lng: '',
				city: '',
				startTime: "00:00",
				startDefaultTime: "06:00",
				endTime: "24:00",
				endDefaultTime: "06:00",
				weekTime1:"08:00",
				weekTime2:"23:00",
				dayTime1:"08:00",
				dayTime2:"23:00",

			};
		},
		onLoad(option) {

		},

		onShow() {
			this.getInit();
		},
		methods: {
			
			clickWeek(){
				this.$refs['time'].open();
			},
			clickDay(){
				this.$refs['time2'].open();
			},
			

			takeSwitch(e) {
				console.info(e);
				this.is_check = e.detail.value;
				if (this.is_check == true) {
					this.is_take = 1;
				} else {
					this.is_take = 0;
				}
			},
			
			
			startRange2(e){
				this.time2 = e[0]+"-"+e[1];
			},
			startRange(e) {
				this.time = e[0]+"-"+e[1];
				console.log(e);
			},

			chooseAddress() {
				let that = this;
				uni.chooseLocation({
					success: function(res) {
						console.info(res)
						that.lat = res.latitude;
						that.lng = res.longitude;

						let regex =
							"(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)"
						let newAddress = res.address.match(regex);
						let group = newAddress.groups;


						that.city = group.city;

						that.address = res.name;
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
					}
				})
			},


			getInit() {
				let that = this;
				let param = {

				};

				this.$api.assistantSetDetail(param).then(res => {

					console.info(JSON.stringify(res));
					if (res.code == 1) {

						that.address = res.data.address,
						that.price = res.data.price,
						that.time = res.data.time,
						that.lat = res.data.lat,
						that.lng = res.data.lng;
							that.time1 = res.data.time2,
							that.mobile = res.data.mobile,
							that.is_open = res.data.is_open;
						if (that.is_open == 1) {
							that.is_check = true;
						} else {
							that.is_check = false;
						}

					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})

			},

			
			updateInfo() {
				let that = this;
				let param = {
					lat: this.lat,
					lng: this.lng,
					city:this.city,
					address:this.address,
					is_open:this.is_take,
					time:this.time,
					time2:this.time1,
					mobile:this.mobile
				}
				
				if(this.mobile == ''){
					
					uni.showToast({
						title:"请填写接单电话",
						icon:'none'
					})
					return;
				}
				
				if(this.lat == '') {
					uni.showToast({
						title:"请选择位置",
						icon:'none'
					})
					return;
				}
				if(this.time == '' || this.time1 == '') {
					uni.showToast({
						title:"请选择时间",
						icon:'none'
					})
					return;
				}
				
				this.$api.assistantSetUpdate(param).then(res => {

					if (res.code == 1) {
						uni.navigateBack({delta:1})
					} else {
						uni.showToast({
							title:res.msg
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		.w48 {
			width: 96rpx;
			height: 96rpx;
		}

		.w36 {
			width: 72rpx;
			height: 72rpx;
		}

		.imgbox {
			width: 164rpx;
			height: 164rpx;
			background-color: #E8E8E8;
			position: relative;

			image {
				width: 100%;
				height: 100%;
			}

			.del {
				width: 36rpx;
				height: 36rpx;
				background: rgba(181, 180, 180, .8);
				position: absolute;
				top: -12rpx;
				right: -12rpx;
			}
		}


	}


	.popup-content2 {
		background: #FFFFFF;

		.popup-btn {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
		}

		.button {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
			font-size: 11pt;
		}

		button::after {
			border: none;
		}

		.pt20 {
			border-bottom: 20rpx solid #f5f5f5;
		}
	}
</style>